package cn.tedu.dictionary.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RedisScanService {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    public List<String> scanKeys(String matchPattern){
        List<String> keys=new ArrayList<>();
        RedisConnection connection = redisConnectionFactory.getClusterConnection();
        ScanOptions scanOptions=ScanOptions.scanOptions().match(matchPattern).build();
        Cursor<byte[]> scan = connection.scan(scanOptions);
        while (scan.hasNext()){
            byte[] next = scan.next();
            String key = new String(next);
            keys.add(key);
        }
        connection.close();
        return keys;
    }
}
